package net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;

public class UDPechoClient {

    private static final int PORT = 4862;
    private static final String hostDomain = "localhost";
    private static final String hostIP = "127.0.0.1";
    public static void main(String[] args) throws IOException {
        System.out.println("UDPClient has already started...");
        System.out.println("--------------------------------");
        //构造接收端的地址SocketAddress，包含主机域名/IP和端口号
        SocketAddress address = new InetSocketAddress(hostIP, PORT);
        //创建数据报，传入地址
        DatagramSocket socket = new DatagramSocket(address);
        //将字符串转为字节格式后传入数据报中，同时传入Socket地址
        String request = "Test UDP client-server";
        byte[] data = request.getBytes();
        DatagramPacket packet = new DatagramPacket(data, data.length, address);
        //通过socket发送packet
        socket.send(packet);
        System.out.println("Request has bean sent by UDPclient...");
        System.out.println("--------------------------------");
}

}
